package malaria_acd;

import database.DatabaseAdapter;
import project.malaria.malaria_acd.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class ConfigActivity extends Activity {
	private Spinner select_sync;
	private Cursor curProvince;
	@SuppressWarnings("unused")
	private String search_id,search_id2,select_province,select_province_id;
	@Override
	protected void onCreate(  Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_config);
		select_sync = (Spinner) findViewById(R.id.spinner1);
		DatabaseAdapter db = new DatabaseAdapter(this);
		db.openWriteDataBase();
		curProvince = db.getCursorProvince();
		
		SimpleCursorAdapter adapter;
		adapter = new SimpleCursorAdapter(this, R.layout.layout_spinner_small, curProvince,new String[] {"PROVINCE_NAME_TH"},new int[] {android.R.id.text1});
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		select_sync.setAdapter(adapter);
		select_sync.setOnItemSelectedListener(new OnItemSelectedListener() {
			
			@SuppressWarnings("unused")
			public void onItemSelected(AdapterView<?> paramAdapterView,
					View paramView, int paramInt, long paramLong) {
				// TODO Auto-generated method stub
				Object item = paramAdapterView.getItemAtPosition(paramInt);
				search_id = ""+paramLong;
				search_id2 = search_id;
				select_province ="";
	   			if(curProvince.moveToPosition(paramInt)){
	   				select_province = curProvince.getString(curProvince.getColumnIndex("PROVINCE_NAME_TH"));
	   				select_province_id = curProvince.getString(curProvince.getColumnIndex("_id"));
	   			}
   			}

			public void onNothingSelected(AdapterView<?> paramAdapterView) {
				// TODO Auto-generated method stub
				
			}
		});
       
       db.close();
   	
	}
	
	public void funcBack(){
		Intent intent = new Intent(this, SyncActivity.class);
		finish();
		startActivityForResult(intent, 0);
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event){
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			funcBack();
			return true;
		}
		return false;
	}
}
